﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;

namespace BLLA
{
    public partial class Order_Walt_T : BaseBLL<Model.Order_Walt_T>, IBLL.IOrder_Walt_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();

        /// <summary>
        /// 获取钱包列表
        /// </summary>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetOrder_Walt(string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.Order_Walt_T> ls = new List<Model.Order_Walt_T>();
                ls = base.GetListBy(u => u.WaltState == 1);
                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = ls;
                    result.total = ls.Count;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("获取钱包列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        /// <summary>
        /// 更新钱包信息
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="json">更新信息</param>
        /// <param name="tag">操作标签（1：新增；2：修改；3：删除）</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string SetOrder_Walt(int OperID, string json, int tag, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                Model.Order_Walt_T info = ParseClass<Model.Order_Walt_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        T = "新增";
                        info.WaltID = GetSysKey.GetKey("WaltKey");
                        info.WaltLastOper = OperID;
                        info.WaltLastTime = new PublicMethod().getDateTimeNow();
                        info.WaltState = 1;
                        ret = base.Add(info);
                        if (ret > 0)
                        {
                            List<Model.Order_UserWalt_T> UerWaltList = new List<Model.Order_UserWalt_T>();
                            List<int> AccList = base.DBSession.IView_UserDAL.GetListBy(u => u.DepID.StartsWith(info.WaltDep)).Select(u => u.AccID).ToList();
                            foreach (var item in AccList)
                            {
                                Model.Order_UserWalt_T uw = new Model.Order_UserWalt_T() { AccID = item, WaltID = info.WaltID, WaltNowNub = 0, LastUpTime = info.WaltLastTime, LastUpFlow = "" };
                                UerWaltList.Add(uw);
                            }
                            base.DBSession.IOrder_UserWalt_TDAL.Add(UerWaltList);
                        }
                        break;
                    case 2:
                        List<Model.Order_Walt_T> list_tmp = base.GetListBy(u => u.WaltID == info.WaltID);
                        Model.Order_Walt_T tmp = list_tmp.First();
                        T = "修改";
                        info.WaltLastOper = OperID;
                        info.WaltLastTime = new PublicMethod().getDateTimeNow();
                        info.WaltState = 1;
                        if (info.WaltDep != tmp.WaltDep)
                        {
                            result.BS = "-3";
                            result.Msg = "不允许改变范围";
                            return os.StringOfObject(result, RETID);
                            //if (info.WaltDep.StartsWith(tmp.WaltDep))
                            //{
                            //    List<int> AccList = base.DBSession.IView_UserDAL.GetListBy(u => u.DepID.StartsWith(info.WaltDep)).Select(u => u.AccID).ToList();
                            //    base.DBSession.IView_UserDAL.GetListBy(AccList. u=>u.AccID
                            //}
                        }
                        Help.FuZhi<Model.Order_Walt_T>.FZ(ref tmp, info);
                        ret = base.Modify(tmp);
                        break;
                    case 3:
                        T = "删除";
                        list_tmp = base.GetListBy(u => u.WaltID == info.WaltID);
                        if (list_tmp != null && list_tmp.Count > 0)
                        {
                            tmp = list_tmp.First();
                            tmp.WaltLastOper = OperID;
                            tmp.WaltLastTime = new PublicMethod().getDateTimeNow();
                            tmp.WaltState = 0;
                            ret = base.Modify(tmp);
                        }
                        break;
                }

                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "更改钱包信息";
                    Log.OperationText = T + "钱包信息成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新钱包信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }


        public void CheckWalt(int AccID)
        {
            List<Model.Order_Walt_T> ls = base.GetListBy(u => u.WaltState == 1);
            string str_depID = base.DBSession.IView_UserDAL.GetListBy(u => u.AccID == AccID).First().DepID;

            #region 检查用户的钱包是否完整
            if (str_depID.Trim() != "")
            {
                foreach (var item in ls)
                {
                    string[] DepList = item.WaltDep.Split(',');

                    foreach (var DepId in DepList)
                    {
                        if (DepId.Length <= str_depID.Length)
                        {
                            if (DepId == str_depID.Substring(0, DepId.Length))
                            {
                                List<Model.Order_UserWalt_T> Uw = base.DBSession.IOrder_UserWalt_TDAL.GetListBy(u => u.WaltID == item.WaltID & u.AccID == AccID);
                                if (Uw.Count == 0)
                                { 

                                    Model.Order_UserWalt_T tm = new Model.Order_UserWalt_T();
                                    tm.AccID = AccID;
                                    tm.WaltID = item.WaltID;
                                    tm.WaltNowNub = 0;
                                    tm.LastUpTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                                    tm.BZ = "";
                                    tm.LastUpFlow = "";
                                    base.DBSession.IOrder_UserWalt_TDAL.Add(tm);
                                }
                            }
                        }
                    }
                }
            }
            #endregion
        }

        #region 获取用户能够使用的钱包
        /// <summary>
        /// 获取用户能够使用的钱包
        /// </summary>
        /// <param name="AccID">用户id</param>
        /// <param name="MerID">商户id</param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string GetUserPayWalt(int AccID, string MerID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.PayMethod> PayList = new List<PayMethod>(); //支付方式
                List<Model.Order_Walt_T> ls = new List<Model.Order_Walt_T>();  //钱包信息
                ls = base.GetListBy(u => u.WaltState == 1 & u.WaltMer.Contains(MerID) & u.WaltUsing.Substring(1, 1) == "1");
                string str_depID = base.DBSession.IView_UserDAL.GetListBy(u => u.AccID == AccID).First().DepID;

                #region 检查用户的钱包是否完整 
                if (str_depID.Trim() != "")
                {
                    foreach (var item in ls)
                    {
                        string[] DepList = item.WaltDep.Split(',');

                        foreach (var DepId in DepList)
                        {
                            if (DepId.Length <= str_depID.Length)
                            {
                                if (DepId == str_depID.Substring(0, DepId.Length))
                                {
                                    List<Model.Order_UserWalt_T> Uw = base.DBSession.IOrder_UserWalt_TDAL.GetListBy(u => u.WaltID == item.WaltID & u.AccID == AccID);
                                    if (Uw.Count == 0)
                                    {
                                        //Model.Order_FlowMon_T fm = new Model.Order_FlowMon_T();
                                        //fm.AccID = AccID;
                                        //fm.FlowMonID = GetSysKey.GetKey("FlowMonKey");
                                        //fm.WaltEndNub = 0;
                                        //fm.WaltFlowType = "1A0003";
                                        //fm.WaltFlowTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                                        //fm.WaltID = item.WaltID;
                                        //fm.WaltHaveNub = 0;
                                        //fm.WaltNowNub = 0;
                                        //fm.WaltFlowOper = 0;
                                        //fm.WaltBZ = "增加钱包";
                                        //base.DBSession.IOrder_FlowMon_TDAL.Add(fm);

                                        Model.Order_UserWalt_T tm = new Model.Order_UserWalt_T();
                                        tm.AccID = AccID;
                                        tm.WaltID = item.WaltID;
                                        tm.WaltNowNub = 0;
                                        tm.LastUpTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                                        tm.BZ = "";
                                        tm.LastUpFlow ="";
                                        base.DBSession.IOrder_UserWalt_TDAL.Add(tm);
                                    }
                                }
                            }
                        }
                    }
                } 
                #endregion
                
                for (int i = ls.Count - 1; i > -1; i--)
                {
                    string[] DepList = ls[i].WaltDep.Split(',');

                    foreach (var item in DepList)
                    {
                        bool jg = false;
                        if (item.Length <= str_depID.Length)
                        {
                            if (item == str_depID.Substring(0, item.Length))
                            {
                                jg = true;
                            }
                        }
                        if (jg)
                        {
                            //jg = false;
                            Model.PayMethod pm = new PayMethod();
                            pm.ID = ls[i].WaltID;
                            pm.Name = ls[i].WaltName;
                            pm.PricType = 1;
                            List<Model.Order_MerPricing_T> MerList = new List<Model.Order_MerPricing_T>();
                            string id = ls[i].WaltID;
                            MerList = base.DBSession.IOrder_MerPricing_TDAL.GetListBy(u => u.PricType == 0 & u.PayID == id & u.PricState == 1);// & u.IsOpen==1
                            if (MerList.Count > 0)
                            {
                                pm.PriTxt = MerList[0].PricTxt;
                            }
                            else
                            {
                                pm.PriTxt = "无优惠";
                            }
                            PayList.Add(pm);
                            break;
                        }
                    }
                }
                List<Model.Order_Pay_T> payList = new List<Model.Order_Pay_T>();
                //payList = base.DBSession.IOrder_Pay_TDAL.GetListBy(u => u.PayOpen == 2 | u.PayOpen == 3);
                payList = base.DBSession.IOrder_Pay_TDAL.GetListBy(u => u.PayOpen == 3);
                foreach (var item in payList)
                {
                    Model.PayMethod pm = new PayMethod();
                    pm.ID = item.PayID;
                    pm.PricType = 2;
                    pm.Name = item.PayName;
                    List<Model.Order_MerPricing_T> MerList = new List<Model.Order_MerPricing_T>();
                    MerList = base.DBSession.IOrder_MerPricing_TDAL.GetListBy(u => u.PricType == 1 & u.PayID == item.PayID & u.PricState == 1&u.MerID==MerID);// & u.IsOpen==1
                    if (MerList.Count > 0)
                    {
                        pm.PriTxt = MerList[0].PricTxt;
                    }
                    else
                    {
                        pm.PriTxt = "无优惠";
                    }
                    PayList.Add(pm);
                }
                result.BS = "1";
                result.Msg = "查询成功";
                result.rows = PayList;
                result.total = PayList.Count;
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("获取钱包列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }
        #endregion
    }
}
